#ifndef QCLICKABLEGRAPHICSWEBVIEW_H
#define QCLICKABLEGRAPHICSWEBVIEW_H

#include <QGraphicsWebView>
#include <QPinchGesture>
#include <QDebug>
#include "MeegoSupport.h"

class QClickableGraphicsWebView : public QGraphicsWebView
{
    Q_OBJECT

public:
    QClickableGraphicsWebView(QGraphicsItem * parent = 0)
        : QGraphicsWebView(parent), mousePressed(false) {
    }

protected:
    bool sceneEvent(QEvent *e)
    {

        bool res = false;
        //if(e->type() != 160 && e->type() != 161 && e->type() != 162)
        //    MeegoSupport::MSMessageBox("Event", QString::number(e->type()));

        switch (e->type()) {

        case QEvent::FocusIn:
        case QEvent::FocusOut:
            return false;

        case QEvent::GraphicsSceneMousePress:
            //MeegoSupport::MSMessageBox("press", QString::number(e->type()));
                //if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
                {
                        mousePressed = true;
                        mousePos = static_cast<QMouseEvent *>(e)->pos();
                }
                break;
        case QEvent::GraphicsSceneMouseRelease:
                //MeegoSupport::MSMessageBox("release",  QString::number(e->type()));
                //if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
                {
                        if(mousePressed)
                        {
                                mousePressed = false;
                                emit mouseClicked();
                        }
                }
                break;
        case QEvent::GraphicsSceneMouseMove:
                {
                        QRect r(mousePos, static_cast<QMouseEvent *>(e)->pos());
                        if(abs(r.height()) > 20 || abs(r.width()) > 20)
                                mousePressed = false;
                }
                break;
        default:
                break;
        }

        // prevent text selection and image dragging
        if (e->type() == QEvent::GraphicsSceneMouseMove)
         return true;
        return res ? true : QGraphicsItem::sceneEvent(e);
    }

signals:
    void mouseClicked();
    void scaleUpdate(qreal);

private:
    bool mousePressed;
    QPoint mousePos;
    QGraphicsItem* m_widget;
};

#endif // QCLICKABLEGRAPHICSWEBVIEW_H
